﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.GrippedObjectList.CApSetGripppedObjectsGridValidator
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.GrippedObjectList;

internal class CApSetGripppedObjectsGridValidator : ITxValidator
{
  private TxGripper _gripper;
  private TxObjGridCtrl _grippedObjectsGrid;
  private List<Type> _validTypes;
  private List<Type> _invalidTypes;

  public CApSetGripppedObjectsGridValidator(TxGripper gripper, TxObjGridCtrl grippedObjectsGrid)
  {
    this._gripper = gripper;
    this._grippedObjectsGrid = grippedObjectsGrid;
    this._validTypes = new List<Type>()
    {
      typeof (TxLogicalCollection),
      typeof (ITxComponent),
      typeof (TxGroup),
      typeof (TxPartAppearance),
      typeof (TxCompoundPart),
      typeof (TxPlanningPart),
      typeof (TxPlanningResource),
      typeof (TxCompoundResource)
    };
    this._invalidTypes = new List<Type>()
    {
      typeof (ITxMfgFeature),
      typeof (TxPlcPropertyProjector),
      typeof (TxSweptVolume)
    };
  }

  public bool IsValidObject(ITxObject obj, out string errorMessage)
  {
    errorMessage = "";
    return this.DoesPassFilter(obj, out errorMessage);
  }

  public bool IsValidText(string text, out string errorMessage)
  {
    errorMessage = (string) null;
    TxObjectList txObjectList = (TxObjectList) null;
    if (text != null)
      txObjectList = TxApplication.ActiveDocument.GetObjectsByName(text);
    bool flag;
    if (txObjectList != null && ((Collection<ITxObject>) txObjectList).Count > 0)
    {
      flag = false;
      foreach (ITxObject itxObject in (Collection<ITxObject>) txObjectList)
      {
        if (this.IsValidObject(itxObject, out errorMessage))
        {
          flag = true;
          break;
        }
      }
    }
    else
    {
      errorMessage = StringTable.INVALID_OBJECT_ERROR;
      flag = false;
    }
    return flag;
  }

  public ITxObject GetObject(string text)
  {
    if (string.IsNullOrEmpty(text))
      return (ITxObject) null;
    TxObjectList objectsByName = TxApplication.ActiveDocument.GetObjectsByName(text);
    if (objectsByName == null || ((Collection<ITxObject>) objectsByName).Count == 0)
      return (ITxObject) null;
    foreach (ITxObject itxObject in (Collection<ITxObject>) objectsByName)
    {
      if (this.IsValidObject(itxObject, out string _))
        return itxObject;
    }
    return (ITxObject) null;
  }

  public string GetText(ITxObject obj)
  {
    return CApSetGripppedObjectsUtils.GetObjectName(obj) ?? string.Empty;
  }

  private bool DoesPassFilter(ITxObject pickedObject, out string errorMessage)
  {
    errorMessage = "";
    if (!this.IsValidType(pickedObject))
    {
      errorMessage = StringTable.INVALID_OBJECT_ERROR;
      return false;
    }
    ITxPlanningObject planningPart = CApSetGripppedObjectsUtils.GetPlanningPart(pickedObject);
    if (planningPart == null)
    {
      errorMessage = StringTable.INVALID_OBJECT_ERROR;
      return false;
    }
    ITxPlanningObject planningRepresentation = CApSetGripppedObjectsUtils.GetPlanningRepresentation((ITxObject) this._gripper);
    if (planningPart == planningRepresentation)
    {
      errorMessage = StringTable.GRIPPER_ERROR;
      return false;
    }
    if (!((Collection<ITxObject>) this._grippedObjectsGrid.Objects).Contains((ITxObject) planningPart))
      return true;
    errorMessage = StringTable.DUPLICATE_OBJECT;
    return false;
  }

  private bool IsValidType(ITxObject pickedObject)
  {
    bool flag1 = false;
    foreach (Type validType in this._validTypes)
    {
      if (validType.IsAssignableFrom(pickedObject.GetType()))
      {
        flag1 = true;
        break;
      }
    }
    bool flag2 = false;
    foreach (Type invalidType in this._invalidTypes)
    {
      if (invalidType.IsAssignableFrom(pickedObject.GetType()))
      {
        flag2 = true;
        break;
      }
    }
    return flag1 && !flag2;
  }
}
